/**
 * Copyright(C) 2008 Matthias Braunhofer
 * All rights reserved
 *
 * Created on: 26.07.2008
*/

package it.cums.core.model;

import it.cums.core.util.CustomerManager;

import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

public class Customers {
	private ArrayList<Customer> customers = new ArrayList<Customer>();
	private ArrayList<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();
		
	public ArrayList<Customer> getCustomers() {
		return customers;
	}
	
	public void addCustomer(Customer customer) {
		customers.add(customer);
		customer.setPropertyChangeListeners(listeners);
		notifyListeners();
	}
	
	public void removeCustomer(Customer customer) {
		customers.remove(customer);
		notifyListeners();
	}
	
	public Customers() {
		List<Customer> customerList = CustomerManager.getInstance().listCustomers();
		if (customerList != null) {
			for (Customer customer : customerList) {
				Orders o = new Orders(customer);
				customer.setOrders(o);
				addCustomer(customer);
			}
		}
	}

	private void notifyListeners() {
		for (PropertyChangeListener listener : listeners) {
			listener.propertyChange(null);
		}
	}
	
	public void addChangeListener(PropertyChangeListener listener) {
		listeners.add(listener);
	}
}
